#include "QxColorSelect.h"

#include <QTableWidget>
#include <QHeaderView>
#include <QColor>
#include <QLineEdit>

class QxColorSelect::PrivateData
{
public:
	int m_iconX;
	int m_iconY;
	int m_row;
	int m_column;
	int m_count;
	int m_currentIndex;   //0,1,2 ...

	struct tag_Color {
		QString* name;
		QColor* color;
	}*s_colorList;

	QTableWidget*		m_tableWidget;
};

QxColorSelect::QxColorSelect(int row, int colum, QWidget *parent)
{
	d_data = new PrivateData;

	d_data->m_iconX = 21;
	d_data->m_iconY = 21;
	d_data->m_row = row;
	d_data->m_column = colum;

	initModel();

	d_data->m_tableWidget = new QTableWidget();
	initView(*d_data->m_tableWidget);

	this->setModel(d_data->m_tableWidget->model());
	this->setView(d_data->m_tableWidget);

	QObject::connect(d_data->m_tableWidget, SIGNAL(cellPressed(int, int)), this, SLOT(slot_ColorSelect_cellActivated(int, int)));
}

QxColorSelect::QxColorSelect( QWidget *parent /*= 0*/ )
{
	d_data = new PrivateData;

	d_data->m_row = 8;
	d_data->m_column = 8;
	d_data->m_count = 0;
	d_data->m_iconX = 21;
	d_data->m_iconY = 21;
	d_data->m_currentIndex = 0;
	d_data->s_colorList = NULL;

	//set ComboBox size
	this->setMinimumSize(QSize((d_data->m_iconX + 1)*8, 20));

	initModel();

	d_data->m_tableWidget = new QTableWidget();
	initView(*d_data->m_tableWidget);

	this->setModel(d_data->m_tableWidget->model());
	this->setView(d_data->m_tableWidget);

	QObject::connect(d_data->m_tableWidget, SIGNAL(cellPressed(int, int)), this, SLOT(slot_ColorSelect_cellActivated(int, int)));
}

QxColorSelect::~QxColorSelect()
{
//  	delete d_data->s_colorList->color;
//  	delete d_data->s_colorList->name;
 	delete d_data->s_colorList;
 	delete d_data->m_tableWidget;
 	delete d_data;
}

void QxColorSelect::initModel()
{
	d_data->m_count = 64;
	if(d_data->s_colorList == NULL)
	{
		d_data->s_colorList = new PrivateData::tag_Color;

		static QColor color[] = {
			QColor(QRgb(0x000000)),	QColor(0x000080),   QColor(0x800080),   QColor(0x808000),    QColor(0xFFA500),  QColor(0xFF21FF),   QColor(0xFF0000),   QColor(0x800000),
			QColor(0x313131),		QColor(0x0000C6),   QColor(0x7B00FF),   QColor(0x002525),    QColor(0xF9AE00),  QColor(0xFF42FF),   QColor(0xFF4200),   QColor(0xC60000),
			QColor(0x4A4A4A),		QColor(0x0000FF),   QColor(0xB531CE),	QColor(0x008000),    QColor(0xEFBD00),  QColor(0xFF00FF),   QColor(0xFF5A31),   QColor(0xC64200),
			QColor(0x747474),		QColor(0x006BFF),	QColor(0xC64ACE),	QColor(0x009421),    QColor(0xFFCE00),  QColor(0xFF63FF),   QColor(0xFF6B42),   QColor(0xBD6339),
			QColor(0x848484),		QColor(0x0094FF),	QColor(0xD663CE),	QColor(0x00BD4A),    QColor(0xFFD600),  QColor(0xFF84FF),   QColor(0xFF8400),   QColor(0xCD8F61),
			QColor(0xB5B5B5),		QColor(0x00B5FF),	QColor(0xE784CE),	QColor(0x00D65A),    QColor(0xFFE700),  QColor(0xFFADF7),   QColor(0xFF947B),   QColor(0xC69C73),
			QColor(0xC0C0C0),		QColor(0x00DEFF),	QColor(0xEF9CC6),	QColor(0x00FF00),    QColor(0xFFFF00),  QColor(0xFFC6FF),   QColor(0xFFADA5),   QColor(0xD6B584),
			QColor(0xFFFFFF),		QColor(0x00FFFF),	QColor(0xFFB5C6),	QColor(0x84FF00),    QColor(0xFFFF84),  QColor(0xF7EFE7),   QColor(0xFFC6CE),   QColor(0xEFD6AD)
		};
		d_data->s_colorList->color = color;

		static QString name[] = {		
			"Black",		"Blue",			"Purple",			"Khaki",		"Apricot",			"Pink",			"Red",			"Brown",
			"DarkBlack",	"DarkBlue",		"DarkPurple",		"Yellow",		"DarkApricot",		"DarkPink",		"Magenta",		"DarkBrown",
			"LightBlack",	"LightBlue",	"LightPurple",		"Green",		"DarkYellow",		"LightPink",	"LightRed",		"LightBrown",
			"MidBlack",		"MidBlue",		"MidPurpler",		"Turquoise",	"MidYellow",		"MidPink",		"MIdRed",		"MidBrown",
			"Darker",		"Azury",		"TintPurple",		"TintKhaki",	"TintApricot",		"TintPink",		"Orange",		"TintBrown",
			"Gray", 		"GrayBlue",		"GrayPurple",		"GrayKhaki",	"GrayApricot",		"GrayPink",		"GrayRed",		"GrayBrown",
			"LightGray",	"LGrayBlue",	"Plum",				"LGrayGreen",	"Yellow",			"LGrayPink",	"LGrayRed",		"LGrayBrown",
			"Whiter",		"Cyan",			"WhiterPurple",		"WhiterKhaki",	"WhiterApricot",	"WhiterPink",	"WhiterRed",	"WhiterBrown"
		};
		d_data->s_colorList->name = name;
	}
}

void QxColorSelect::initView(QTableWidget &m_color_table)
{
	struct colorbox
	{
		int m_index;
		QColor m_number;
		QString m_name;
	};

	m_color_table.horizontalHeader()->setVisible(FALSE);
	m_color_table.verticalHeader()->setVisible(FALSE);
	m_color_table.setShowGrid(FALSE);
	m_color_table.horizontalHeader()->setDefaultSectionSize(d_data->m_iconX);
	m_color_table.verticalHeader()->setDefaultSectionSize(d_data->m_iconY);
	m_color_table.setRowCount(d_data->m_row);
	m_color_table.setColumnCount(d_data->m_column);

	int m_pos;
	colorbox m_createcolor[64];

	for(int i = 0; i < 64; i++)
	{
		m_createcolor[i].m_index = i;
		m_createcolor[i].m_name = d_data->s_colorList->name[i];
		m_createcolor[i].m_number = d_data->s_colorList->color[i];
	}
	for(int i = 0;i<8;i++)
	{
		for(int j = 0;j<8;j++)
		{
			m_pos = j + i*8;
			QTableWidgetItem* colorItem = new QTableWidgetItem;
			QPixmap pixmap = QPixmap(40,36);
			pixmap.fill(m_createcolor[m_pos].m_number);
			QIcon coloricon = QIcon(pixmap);
			colorItem->setIcon(coloricon);
			//colorItem->setText(QString("%1").arg(colorValue));
			colorItem->setText(tr(m_createcolor[m_pos].m_name.toLocal8Bit().data()));
			colorItem->setData(Qt::UserRole,QVariant((int)&(m_createcolor[m_pos].m_number)));
			m_color_table.setItem(i,j,colorItem);
		}
	}
}

void QxColorSelect::slot_ColorSelect_cellActivated(int row, int column)
{
	QColor colorValue = *(QColor*)(d_data->m_tableWidget->item(row, column)->data(Qt::UserRole).toInt());
	d_data->m_currentIndex = row * 8 + column;
	emit cellPressed(colorValue);
}

//set default color
void QxColorSelect::slot_setComboBox( int index)
{
	QObject::disconnect(d_data->m_tableWidget, SIGNAL(cellPressed(int, int)), this, SLOT(slot_ColorSelect_cellActivated(int, int)));

	int y = index % 8;
	int x = index / 8;
	setModelColumn(y);
	setCurrentIndex(x);

	QObject::connect(d_data->m_tableWidget, SIGNAL(cellPressed(int, int)), this, SLOT(slot_ColorSelect_cellActivated(int, int)));
}

void QxColorSelect::setIconSize( int x, int y )
{
	d_data->m_tableWidget->horizontalHeader()->setDefaultSectionSize(x);
	d_data->m_tableWidget->verticalHeader()->setDefaultSectionSize(y);

	this->setMinimumSize(QSize((x+1)*8, 20));
}

QString QxColorSelect::getName( int index )
{
	if (d_data->s_colorList && index > 0)
	{
		if (d_data->m_count > index)
		{
			return d_data->s_colorList->name[index];
		}
	}
	return "error";
}

QColor QxColorSelect::getColor( int index )
{
	if (d_data->s_colorList && index > 0)
	{
		if (d_data->m_count > index)
		{
			return d_data->s_colorList->color[index];
		}
	}

	return QColor(Qt::black);
}

int QxColorSelect::getCurrentIndex()
{
	return d_data->m_currentIndex;
}

QColor QxColorSelect::getCurrentColor()
{
	int index = getCurrentIndex();
	return getColor(index);
}
